/*******************************************************************************
 * Copyright (c) 2003, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.sequence.sedi.action;

//import java.io.InputStream;
//import java.io.ObjectInputStream;


import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.EditPartFactory;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.editparts.ScalableFreeformRootEditPart;
import org.eclipse.gef.print.PrintGraphicalViewerOperation;
import org.eclipse.gef.ui.parts.ScrollingGraphicalViewer;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.cea.papyrus.diagram.sequence.sedi.edit.SeDiEditPartFactory;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;


/**
 * @author Eric Bordeau
 * modified by Andreas Limyr and frankd@ifi.uio.no
 */
public class SeDiPrintAction 
	extends Action 
	implements IObjectActionDelegate
{
	private Diagram diagram;
	//private IFile selectedFile;
	
	public SeDiPrintAction() {}
	
	protected Diagram getDiagram() {
		return diagram;
	}
	
	protected void setContents(Diagram o) {
		diagram = o;
	}

	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// noop
	}
	
	public void run(IAction action) {
		Shell shell = new Shell();
		GraphicalViewer viewer = new ScrollingGraphicalViewer();
		viewer.createControl(shell);
		viewer.setEditDomain(createEditDomain());
		viewer.setRootEditPart(new ScalableFreeformRootEditPart());
		viewer.setEditPartFactory(createEditPartFactory());
		viewer.setContents(getDiagram());
		viewer.flush();
		
		int printMode = new PrintModeDialog(shell).open();
		if (printMode == -1)
			return;
		PrintDialog dialog = new PrintDialog(shell, SWT.NULL);
		PrinterData data = dialog.open();
		if (data != null) {
			PrintGraphicalViewerOperation op = 
				new PrintGraphicalViewerOperation(new Printer(data), viewer);
			op.setPrintMargin(new Insets(40));
			op.setPrintMode(printMode);
			op.run("Print diagram");
		}
	}
	
	protected DefaultEditDomain createEditDomain() {
		return new DefaultEditDomain(null);
	}
	
	protected EditPartFactory createEditPartFactory() {
		return new SeDiEditPartFactory(null);
	}
	
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Take a look at what happens here. seems like a lot of I/O going on
		// TODO Temporarily disabled due to exceptions
		/*
		if (!(selection instanceof IStructuredSelection))
			return;
		IStructuredSelection sel = (IStructuredSelection)selection;
		if (sel.size() != 1)
			return;
		selectedFile = (IFile)sel.getFirstElement();
		try {
			InputStream is = selectedFile.getContents(false);
			ObjectInputStream ois = new ObjectInputStream(is);
			setContents(ois.readObject());
			ois.close();
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		
	}
}
